<?php

namespace Admin\Form;

use Admin\Validate\UserValidator;
use Zend_Form;

class EditForm extends Zend_Form
{
    private $routeMatch;

    public function __construct($routeMatch)
    {
        $this->routeMatch = $routeMatch;
        parent::__construct();
    }

    public function init()
    {
        $userValidator = new UserValidator($this->routeMatch);
        $this->addElement('text', 'userName', [
            'required'  => true,
            'label'     => 'User Name',
            'filters'   => [
                'StringTrim'
            ],
            'validators' => [
                'Alnum',
                $userValidator
            ],
            'maxLength' => '50'
        ]);
        $this->addElement('password', 'password', [
            'label'    => 'New password',
            'maxLength' => '50'
        ]);
        $this->addElement('password', 'confirmPassword', [
            'label'    => 'Confirm New Password',
            'maxLength' => '50',
            'validators' => [
                [
                    'identical', true,
                    [
                        'password',
                        'messages' => 'Field "Password" must be equal "Confirm New Password"'
                    ]
                ]
            ]
        ]);
        $this->addElement('select', 'role', [
            'required'   => true,
            'label'      => 'Select role',
            'multiOptions' => [
                'administrator' => 'administrator',
                'user'          => 'user'
            ],
            'maxLength'  => '50'
        ]);
        $this->addElement('note', 'note', [
            'name'    => 'note',
            'decorators' => [
                ['ViewHelper'],
                [
                    'HtmlTag', [
                    'tag' => 'br',
                ]
                ]
            ]

        ]);
        $this->addElement('note', 'cancel', [
            'name'    => 'note',
            'value'    => 'Cancel',
            'decorators' => [
                ['ViewHelper'],
                [
                    'HtmlTag', [
                        'tag' => 'a',
                        'href' => '../../users',
                        'class' => 'btn btn-info',
                    ]
                ]
            ]

        ]);
        $this->addElements([
            ['submit', 'submitEditUser', [
                'label'   => 'Save',
                'class'   => 'btn btn-info'
            ]]
        ]);
        $this->setMethod('POST');
    }
}